function [capacity_awgn, spectral_efficiency, fig_awgn] = awgn_capacity(EbN0_dB, bandwidths)
% AWGN信道容量分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   bandwidths - 带宽数组 (Hz)
% 输出参数：
%   capacity_awgn - AWGN信道容量
%   spectral_efficiency - 频谱效率
%   fig_awgn - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
noise_power_dBm = -174; % 热噪声功率谱密度 (dBm/Hz)
noise_power = 10^((noise_power_dBm-30)/10); % 转换为线性值 (W/Hz)

% 初始化结果数组
num_snr = length(EbN0_dB);
num_bandwidths = length(bandwidths);
capacity_awgn = zeros(num_snr, num_bandwidths);
spectral_efficiency = zeros(num_snr, num_bandwidths);

fprintf('AWGN信道容量分析...\n');

% AWGN容量计算 (Shannon公式)
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10); % SNR线性值
    
    for bw_idx = 1:num_bandwidths
        bandwidth = bandwidths(bw_idx);
        
        % Shannon容量公式：C = B * log2(1 + S/N)
        % 其中 S/N = Eb/N0 * R/B，这里简化为直接SNR
        if snr_linear > 0
            capacity_awgn(snr_idx, bw_idx) = bandwidth * log2(1 + snr_linear);
            spectral_efficiency(snr_idx, bw_idx) = log2(1 + snr_linear); % bps/Hz
        else
            capacity_awgn(snr_idx, bw_idx) = 0;
            spectral_efficiency(snr_idx, bw_idx) = 0;
        end
    end
end

%% 可视化结果
fig_awgn = figure('Name', 'AWGN信道容量分析', 'Position', [100, 100, 1200, 800]);

% 子图1: 容量 vs SNR
subplot(2, 2, 1);
for bw_idx = 1:num_bandwidths
    plot(EbN0_dB, capacity_awgn(:, bw_idx)/1e6, 'LineWidth', 2, 'Color', colors(bw_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('AWGN信道容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%.1f MHz', x/1e6), bandwidths, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 频谱效率 vs SNR
subplot(2, 2, 2);
for bw_idx = 1:num_bandwidths
    plot(EbN0_dB, spectral_efficiency(:, bw_idx), 'LineWidth', 2, 'Color', colors(bw_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('频谱效率 (bps/Hz)');
title('AWGN信道频谱效率 vs 信噪比');
legend(arrayfun(@(x) sprintf('%.1f MHz', x/1e6), bandwidths, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图3: 容量 vs 带宽 (固定SNR)
subplot(2, 2, 3);
snr_index = round(num_snr/2); % 中等SNR
plot(bandwidths/1e6, capacity_awgn(snr_index, :)/1e6, 'o-', 'LineWidth', 2, 'Color', colors(1, :));
grid on;
xlabel('带宽 (MHz)');
ylabel('容量 (Mbps)');
title(sprintf('AWGN信道容量 vs 带宽 (Eb/N0 = %d dB)', EbN0_dB(snr_index)));

% 子图4: Shannon限界
subplot(2, 2, 4);
% 绘制-1.59 dB的Shannon限界
shannon_limit = -1.59; % Shannon限界 (dB)
x_range = min(EbN0_dB):0.1:max(EbN0_dB);
y_range = log2(1 + 10.^(x_range/10));

plot(x_range, y_range, 'k-', 'LineWidth', 2);
hold on;
plot(EbN0_dB, spectral_efficiency(:, 1), 'LineWidth', 2, 'Color', colors(1, :));
plot([shannon_limit, shannon_limit], [0, max(y_range)], 'r--', 'LineWidth', 2);
grid on;
xlabel('Eb/N0 (dB)');
ylabel('频谱效率 (bps/Hz)');
title('AWGN信道Shannon限界');
legend('Shannon公式', '仿真结果', 'Shannon限界 (-1.59 dB)', 'Location', 'NorthWest');

fprintf('AWGN容量分析完成！\n');

end